﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Product_BulkCreate
    Inherits System.Web.UI.UserControl
    Dim level As String = 0
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            getMenu()

            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
        End If
    End Sub
    
    Protected Sub getMenu()
        ddlType.Items.Clear()
        ddlType.AppendDataBoundItems = True
        ddlType.Items.Insert(0, New ListItem("---Chọn---", "0"))
        Using data As New VSEntities
            'Dim p = (From h In data.StoreTypes Where h.StoreID = ddlStore.SelectedValue Select h.RootCateId).Distinct.ToArray
            Dim query As List(Of Category) = (From q In data.Categories Where q.ParentId = 0 Order By q.Sort Ascending Select q).ToList
            ddlType.DataValueField = "CatId"
            ddlType.DataTextField = "Title"
            If Not query Is Nothing Then
                For i As Integer = 0 To query.Count - 1
                    ddlType.Items.Add(New ListItem(query.Item(i).Title, query.Item(i).CatId))
                    RecursiveFillTree(query, query.Item(i).CatId)
                Next
            End If
        End Using
    End Sub
    Private Sub RecursiveFillTree(ByVal dtParent As List(Of Category), ByVal parentID As Integer)
        level += 1
        'on the each call level increment 1
        Dim appender As New StringBuilder()

        For j As Integer = 0 To level - 1

            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
        Next
        If level > 0 Then
            appender.Append("|__")
        End If

        Using data As New VSEntities
            Dim dv As List(Of Category) = (From q In data.Categories Where q.ParentId = parentID Select q).ToList
            Dim i As Integer
            If dv.Count > 0 Then
                For i = 0 To dv.Count - 1
                    Dim itm As New ListItem(Server.HtmlDecode(appender.ToString() + dv.Item(i).Title.ToString()), dv.Item(i).CatId.ToString())
                    ddlType.Items.Add(itm)
                    RecursiveFillTree(dtParent, Integer.Parse(dv.Item(i).CatId.ToString()))
                Next
            End If
        End Using
        level -= 1
        'on the each function end level will decrement by 1
    End Sub

#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim arrImage() As String = hidStrImage.Text.Trim.Split(",")
        Dim arrMota() As String = hidStrCommnet.Text.Trim.Split(";")

        Dim arrMaSanPham() As String = hidStrMaSanPham.Text.Trim.Split(";")
        Dim arrGiaSanPham() As String = hidstrGiaSanPham.Text.Trim.Split(";")
        Dim arrProductCate() As String = hidStrProductCate.Text.Trim.Split(";")

        Using data As New VSEntities

            Try

                For i As Integer = 0 To arrImage.Length - 2
                    Dim p As New Product
                    p.Title = arrMaSanPham(i)
                    p.QueryString = makeURLFriendly(arrMaSanPham(i))
                    p.Description = arrMota(i)
                    p.ImagePreview = arrImage(i)
                    p.OriginalPrice = Math.Round(Decimal.Parse(arrGiaSanPham(i)), 2)
                    p.SellPrice = p.OriginalPrice
                    p.Created = Now
                    p.CreatorId = Session("UserID")
                    p.IsSellOff = False
                    p.IsHot = False
                    p.StatusId = 1
                    p.CateID = arrProductCate(i)
                    p.TinhTrangHang = 0
                    p.Code = arrMaSanPham(i)
                    p.TongSo = 0
                    p.DaBan = 0

                    data.Products.AddObject(p)
                    data.SaveChanges()
                Next




                'Insert_App_Log("Insert Product:" & txtTitle.Text.Trim & "", Function_Name.Product, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Product/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub


#End Region

    
    

End Class
